package data;

import isg3.AdaptadorBoolean;
import isg3.AdaptadorFecha;
import isg3.Comentario;
import isg3.Evento;
import isg3.GestionDeComentarios;
import isg3.GestiondeEventos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import utils.UIDGenerator;

public class JDBCComentarioDAO implements IComentarioDAO {
	private Connection conn;
	
	protected void finalize() {
        ConnectionManager.getInstance().checkIn(conn);
    }

	public Comentario select(Connection conn, String ComentarioOID) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        Comentario c = null;
        String sql = "SELECT * FROM Comentarios WHERE (OIDcomentario = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, ComentarioOID);
            result = stmt.executeQuery();

            result.next();
            c.setIDComentario(result.getString("IDcomentario"));
            c.setMensaje(result.getString("Mensaje"));
            c.setVotoNegativo(result.getInt("votoNegativo"));
            c.setVotoNegativo(result.getInt("votoPositivo"));
            c.setUsuario(result.getString("IDusuario"));
            
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return c;
	}


	public List selectAllComentarios() {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;
        String sql = "SELECT * FROM Comentarios";
        try {

            
            stmt = conn.prepareStatement(sql); //conn es una conexión válida
            result = stmt.executeQuery();

            while (result.next()) {
            	Comentario c = new Comentario();
                c.setIDComentario(result.getString("IDcomentario"));
                c.setMensaje(result.getString("Mensaje"));
                c.setVotoNegativo(result.getInt("votoNegativo"));
                c.setVotoNegativo(result.getInt("votoPositivo"));
                c.setfecha(AdaptadorFecha.convierteCadenaCalendar(result.getString("fecha")));
                searchResults.add(c);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;

	}

	
	public String selectComentarioOID( String IDComentario) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        String oidc = null;
        String sql = "SELECT * FROM Comentarios WHERE (IDcomentario = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, IDComentario);
            result = stmt.executeQuery();

            result.next();
            oidc = result.getString("OIDcomentario");
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return oidc;
		
	}

	
	public void insert(Comentario c,String idp,String ide) {
	JDBCPersonaDAO perdao= new JDBCPersonaDAO();
	String oidp=perdao.selectPersonaOID(idp);
	System.out.println(oidp);
	String oide= GestiondeEventos.getInstance().getEventoOID(ide);
        PreparedStatement stmt = null;
        String sql = "INSERT INTO Comentarios ( OIDcomentario, OIDpersona, OIDevento, IDcomentario, Mensaje, votoPositivo, votoNegativo, fecha) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ";
        try {
        	conn = ConnectionManager.getInstance().checkOut();
        	System.out.println(sql);
            stmt = conn.prepareStatement(sql);
            String comentarioOID=UIDGenerator.getInstance().getKey();
            String personaOID=oidp;
            String eventoOID=oide;
            stmt.setString(1, comentarioOID);
            stmt.setString(2, personaOID);
            stmt.setString(3, eventoOID);
            stmt.setString(4, c.getIDComentario() );
            stmt.setString(5, c.getMensaje());
            stmt.setInt(6, c.getVotoPositivo());
            stmt.setInt(7,c.getVotoNegativo());
            stmt.setString(8, AdaptadorFecha.convierteCalendarCadena(c.getFecha()));

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}
	public void update(Comentario c,String OIDcomentario) {
		
        PreparedStatement stmt = null;
        String sql = "UPDATE Comentarios SET votoPositivo = ?, votoNegativo = ? WHERE (OIDcomentario= ?)";
        try {
        	conn = ConnectionManager.getInstance().checkOut();
        	System.out.println(sql);
            stmt = conn.prepareStatement(sql);

            stmt.setString(3,OIDcomentario);
            stmt.setInt(1, c.getVotoPositivo());
            stmt.setInt(2,c.getVotoNegativo());
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}

	
	public String getPersonaOID(String comentarioOID) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        String oidp = null;
        String sql = "SELECT OIDpersona FROM Comentarios WHERE (OIDcomentario = ?) ";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, comentarioOID);
            result = stmt.executeQuery();

            result.next();
            oidp = result.getString("OIDpersona");
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return oidp;
		
	}
	public String getPersonaOID2(String personaID) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        String oidp = null;
        String sql = "SELECT OIDpersona FROM Persona WHERE (NIF = ?) ";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, personaID);
            result = stmt.executeQuery();

            result.next();
            oidp = result.getString("OIDpersona");
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return oidp;
		
	}

	
	public List selectAllComentarios(String OIDevento) {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;
        String sql = "SELECT * FROM Comentarios where OIDevento=?";
        try {

            
            stmt = conn.prepareStatement(sql); //conn es una conexión válida
            stmt.setString(1, OIDevento);
            result = stmt.executeQuery();

            while (result.next()) {
            	Comentario c = new Comentario();
                c.setIDComentario(result.getString("IDcomentario"));
                c.setMensaje(result.getString("Mensaje"));
                c.setVotoNegativo(result.getInt("votoNegativo"));
                c.setVotoNegativo(result.getInt("votoPositivo"));
                c.setfecha(AdaptadorFecha.convierteCadenaCalendar(result.getString("fecha")));
                searchResults.add(c);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

}
